package com.gx.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gx on 2017/1/20.
 */
@Controller
@RequestMapping(value = "/shiro",produces={"text/html;charset=UTF-8;"})
public class ShiroController {

    @RequestMapping("/dologin")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            //执行认证操作.
            subject.login(token);
        }catch (AuthenticationException ae) {
            System.out.println("登陆失败: " + ae.getMessage());
            return "/login";
        }

        return "/success";
    }

    @RequestMapping("/login")
    public String list(){
        System.out.println("...login...");
        return "login";
    }


    @RequestMapping("/user")
    public String user(){
        System.out.println("...login...");
        return "user";
    }
    @RequestMapping("/admin")
    public String admin(){
        System.out.println("...login...");
        return "admin";
    }

    @RequestMapping("/unauthor")
    public String unauthor(){
        System.out.println("...login...");
        return "unauthor";
    }
    @RequestMapping("/countries")
    public String logout(){
        System.out.println("...login...");
        return "/shiro/login";
    }


}
